package com.m4f.city.web.controller;

import java.security.Principal;
import java.util.List;
import java.util.Locale;
import java.util.logging.Logger;

import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.m4f.city.business.domain.InternalUser;
import com.m4f.city.business.domain.oauth2.OAuth2AccessTokenDS;
import com.m4f.city.business.domain.oauth2.OAuth2RefreshTokenDS;
import com.m4f.city.business.security.InternalUserDetailService;
import com.m4f.gaeweb.exception.M4FException;

@Controller
@RequestMapping("/")
public class HomeController extends BaseController {
	private static final Logger LOGGER = Logger.getLogger(HomeController.class.getName());
	
	@RequestMapping(method=RequestMethod.GET)
	public String getIndex(Principal principal, Locale locale) throws M4FException {
		if(principal != null) {
			InternalUser user = getInternalUser(principal);
			if(user.getAuthorities() != null) {
				if(user.getAuthorities().contains(new GrantedAuthorityImpl(InternalUserDetailService.ROLE_ROOT))) {
					return getRedirect("/admin/home", locale);
				} else if(user.getAuthorities().contains(new GrantedAuthorityImpl(InternalUserDetailService.ROLE_MANAGER))) {
					return getRedirect("/manager/home", locale);
				} else if(user.getAuthorities().contains(new GrantedAuthorityImpl(InternalUserDetailService.ROLE_USER))) {
					return getRedirect("/user/home", locale);
				}
			}
			return getRedirect("/", locale);
		} else {
			return "index";
		}
	}
	
	/*@RequestMapping("/home")
	public String getHome(Principal principal, Model model) {
		model.addAttribute("user", principal);
		return "index";
	}*/
	
	@RequestMapping("/login")
	public String getLogin() {
		return "login";
	}
	
	// TODO TEMP to remove the oauth tokens
	@RequestMapping("/inittokens")
	public String init() {
		try {
			List<OAuth2AccessTokenDS> accessList = this.serviceLocator.getOAuth2TokensService().getAllAccessToken();
			List<OAuth2RefreshTokenDS> refreshList = this.serviceLocator.getOAuth2TokensService().getAllRefreshToken();
			
			for(OAuth2AccessTokenDS token : accessList) this.serviceLocator.getOAuth2TokensService().removeAccessToken(token);
			for(OAuth2RefreshTokenDS token : refreshList) this.serviceLocator.getOAuth2TokensService().removeRefreshToken(token);
		} catch(Exception e) {
			e.printStackTrace();
			return "common.error";
		}
		
		return "home";
		
	}
	
	/*
	@RequestMapping("/loginoauth")
	public String getLogin(@RequestParam String client_id, HttpSession session, Model model) {
		try {
			if(client_id == null || ("").equals(client_id)) {
				VerificationCodeAuthenticationToken token = (VerificationCodeAuthenticationToken) session.getAttribute("org.springframework.security.oauth2.provider.webserver.DefaultClientAuthenticationCache#SAVED_AUTH");
				client_id = token.getClientId();
			}
			Application app = this.serviceLocator.getApplicationService().getAppliaction(client_id);
			model.addAttribute("client", app);
		} catch(Exception e) {
			LOGGER.log(Level.SEVERE, StackTraceUtil.getStackTrace(e)); // TODO redirect to oauth page error
		}
		return "oauth.access.loginconfirm";
	}*/
}
